package com.litong.litongjiaoyu.controller;

import com.litong.litongjiaoyu.entity.SysCampus;
import com.litong.litongjiaoyu.entity.SysCourse;
import com.litong.litongjiaoyu.service.CampusService;
import com.litong.litongjiaoyu.service.CourseService;
import com.litong.litongjiaoyu.utils.RedisUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName ScheduledTasks
 * @Description 定时任务管理层
 * @Author jingz
 * @Date 2019/11/14 13:56
 * @Version 1.0
 **/
@Log4j2
@Component
@EnableScheduling
public class ScheduledTasks {
    /**
     * 课程服务层
     */
    @Autowired
    private CourseService courseService;

    /**
     * 引入Redis操作工具类
     */
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 引入获取token工具
     */
    @Autowired
    private WxPushServiceQcl wxPushServiceQcl;

    /**
     * 获取校区
     */
    @Autowired
    private CampusService campusService;


    /**
     * 将推荐课程放入redis中
     */
    @Scheduled(fixedDelay = 29500)
    public void getRecommend() {
        log.info("将推荐课程信息存放在Redis");

        List<SysCourse> coursetable = courseService.getRecommend();


        for (SysCourse sysCourse : coursetable) {
            redisUtil.lSet("Recommend", sysCourse, 29);
        }



        log.info("完成任务：推荐课程信息存放在Redis");
    }

    /**
     * 将精品课程放入redis中
     */
    @Async
    @Scheduled(fixedDelay = 29500)
    public void getExcellentCourses() {

        log.info("将精品课程信息存放在Redis");

        List<SysCourse> coursetable = courseService.getExcellentCourses();


        for (SysCourse sysCourse : coursetable) {
            redisUtil.lSet("ExcellentCourses", sysCourse, 29);
        }


        log.info("完成任务：精品课程信息存放在Redis");
    }

    /**
     * 将精品课程放入redis中
     */
    @Async
    @Scheduled(fixedDelay = 29500)
    public void getAudition() {

        log.info("将试听课程信息存放在Redis");

        List<SysCourse> coursetable = courseService.getAudition();


        for (SysCourse sysCourse : coursetable) {
            redisUtil.lSet("Audition", sysCourse, 29);
        }


        log.info("完成任务：试听课程存放在Redis");
    }

    /**
     * 每隔两小时获取一次token
     */
    @Async
    @Scheduled(fixedDelay = 2 * 60 * 60 * 1000)
    public void getToken() {
        log.info("获取token存放在Redis");

        String access_Token = wxPushServiceQcl.getAccess_token("wx98b1240134c0104b", "17f768b51905ff61a268412d0040805a");

        if(access_Token != null) {
            try {
                //每隔两小时获取一次存放
                redisUtil.set("Token", access_Token, 2 * 60 * 60);
                log.info("完成任务：获取token存放在Redis");
            } catch (Exception e) {
                log.info("获取token存放在Redis失败：" + e.getMessage());
            }
        } else {
            log.info("获取到的token为空");
        }
    }

    /**
     * 校区信息放入redis
     */
    @Async
    @Scheduled(fixedDelay = 29500)
    public void getCampus() {
        log.info("开始获取校区所有信息");
        List<SysCampus> campuses = campusService.query();

        for (SysCampus campus : campuses) {
            redisUtil.lSet("Campus", campus, 29);
        }

        log.info("完成任务：获取校区所有信息");
    }

    /**
     * 剩余课时提醒
     * 每天早上7点，中午12点，晚上18点发送
     * 将检查到的放在redis中，一天只发送一次
     */
    // @Async
    // @Scheduled(cron = "0 0 7,12,18 * * ? ")
    // public void reminderOfRemainingClass () {
    //
    // }
}
